load("@rules_proto//proto:defs.bzl", "proto_library")
load("@bazel_gomock//:gomock.bzl", "gomock")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
    name = "rpc",
    srcs = [
        "addjoin.go",
        "auth.go",
        "auth_tenant.go",
        "client.go",
        "clock_offset.go",
        "codec.go",
        "connection.go",
        "connection_class.go",
        "context.go",
        "context_testutils.go",
        "errors.go",
        "heartbeat.go",
        "keepalive.go",
        "metrics.go",
        "peer.go",
        "peer_map.go",
        "restricted_internal_client.go",
        "settings.go",
        "snappy.go",
        "tls.go",
    ],
    embed = [":rpc_go_proto"],
    importpath = "github.com/cockroachdb/cockroach/pkg/rpc",
    visibility = ["//visibility:public"],
    deps = [
        "//pkg/base",
        "//pkg/clusterversion",
        "//pkg/keys",
        "//pkg/kv/kvpb",
        "//pkg/multitenant/tenantcapabilities",
        "//pkg/roachpb",
        "//pkg/rpc/rpcpb",
        "//pkg/security",
        "//pkg/security/certnames",
        "//pkg/security/username",
        "//pkg/settings",
        "//pkg/settings/cluster",
        "//pkg/ts/tspb",
        "//pkg/util",
        "//pkg/util/circuit",
        "//pkg/util/envutil",
        "//pkg/util/growstack",
        "//pkg/util/grpcutil",
        "//pkg/util/hlc",
        "//pkg/util/log",
        "//pkg/util/log/logcrash",
        "//pkg/util/log/severity",
        "//pkg/util/metric",
        "//pkg/util/metric/aggmetric",
        "//pkg/util/netutil",
        "//pkg/util/netutil/addr",
        "//pkg/util/stop",
        "//pkg/util/syncutil",
        "//pkg/util/timeutil",
        "//pkg/util/tracing",
        "//pkg/util/tracing/grpcinterceptor",
        "//pkg/util/uuid",
        "@com_github_cockroachdb_errors//:errors",
        "@com_github_cockroachdb_logtags//:logtags",
        "@com_github_cockroachdb_redact//:redact",
        "@com_github_gogo_protobuf//proto",
        "@com_github_golang_protobuf//proto:go_default_library",
        "@com_github_golang_snappy//:snappy",
        "@com_github_montanaflynn_stats//:stats",
        "@com_github_vividcortex_ewma//:ewma",
        "@io_opentelemetry_go_otel//attribute",
        "@org_golang_google_grpc//:go_default_library",
        "@org_golang_google_grpc//backoff",
        "@org_golang_google_grpc//codes",
        "@org_golang_google_grpc//connectivity",
        "@org_golang_google_grpc//credentials",
        "@org_golang_google_grpc//credentials/insecure",
        "@org_golang_google_grpc//encoding",
        "@org_golang_google_grpc//keepalive",
        "@org_golang_google_grpc//metadata",
        "@org_golang_google_grpc//peer",
        "@org_golang_google_grpc//stats",
        "@org_golang_google_grpc//status",
    ],
)

gomock(
    name = "mock_rpc",
    out = "mocks_generated_test.go",
    interfaces = ["Dialbacker"],
    library = ":rpc",
    package = "rpc",
    self_package = "github.com/cockroachdb/cockroach/pkg/rpc",
    visibility = [
        ":__pkg__",
        "//pkg/gen:__pkg__",
    ],
)

go_test(
    name = "rpc_test",
    size = "small",
    srcs = [
        "auth_test.go",
        "clock_offset_test.go",
        "codec_test.go",
        "context_test.go",
        "datadriven_test.go",
        "down_node_test.go",
        "heartbeat_test.go",
        "helpers_test.go",
        "main_test.go",
        "metrics_test.go",
        "peer_test.go",
        "snappy_test.go",
        "tls_test.go",
        ":mock_rpc",  # keep
    ],
    data = glob(["testdata/**"]),
    embed = [":rpc"],
    exec_properties = select({
        "//build/toolchains:is_heavy": {"test.Pool": "large"},
        "//conditions:default": {"test.Pool": "default"},
    }),
    deps = [
        "//pkg/base",
        "//pkg/clusterversion",
        "//pkg/keys",
        "//pkg/kv/kvpb",
        "//pkg/multitenant/tenantcapabilities",
        "//pkg/multitenant/tenantcapabilities/tenantcapabilitiesauthorizer",
        "//pkg/raft/raftpb",
        "//pkg/roachpb",
        "//pkg/security",
        "//pkg/security/certnames",
        "//pkg/security/securityassets",
        "//pkg/security/securitytest",
        "//pkg/security/username",
        "//pkg/settings",
        "//pkg/settings/cluster",
        "//pkg/spanconfig",
        "//pkg/testutils",
        "//pkg/testutils/datapathutils",
        "//pkg/testutils/skip",
        "//pkg/ts/tspb",
        "//pkg/util",
        "//pkg/util/circuit",
        "//pkg/util/grpcutil",
        "//pkg/util/hlc",
        "//pkg/util/leaktest",
        "//pkg/util/log",
        "//pkg/util/netutil",
        "//pkg/util/stop",
        "//pkg/util/syncutil",
        "//pkg/util/timeutil",
        "//pkg/util/tracing",
        "//pkg/util/uuid",
        "@com_github_cockroachdb_datadriven//:datadriven",
        "@com_github_cockroachdb_errors//:errors",
        "@com_github_cockroachdb_redact//:redact",
        "@com_github_gogo_status//:status",
        "@com_github_golang_mock//gomock",
        "@com_github_prometheus_client_model//go",
        "@com_github_stretchr_testify//assert",
        "@com_github_stretchr_testify//require",
        "@org_golang_google_grpc//:go_default_library",
        "@org_golang_google_grpc//codes",
        "@org_golang_google_grpc//credentials",
        "@org_golang_google_grpc//health/grpc_health_v1",
        "@org_golang_google_grpc//metadata",
        "@org_golang_google_grpc//peer",
        "@org_golang_google_grpc//status",
    ],
)

proto_library(
    name = "rpc_proto",
    srcs = ["heartbeat.proto"],
    strip_import_prefix = "/pkg",
    visibility = ["//visibility:public"],
    deps = [
        "//pkg/roachpb:roachpb_proto",
        "@com_github_gogo_protobuf//gogoproto:gogo_proto",
    ],
)

go_proto_library(
    name = "rpc_go_proto",
    compilers = ["//pkg/cmd/protoc-gen-gogoroach:protoc-gen-gogoroach_grpc_compiler"],
    importpath = "github.com/cockroachdb/cockroach/pkg/rpc",
    proto = ":rpc_proto",
    visibility = ["//visibility:public"],
    deps = [
        "//pkg/roachpb",
        "@com_github_gogo_protobuf//gogoproto",
    ],
)
